package com.project.petLossSystem.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/*
* 复制bean
* */
public class MyBeanUtil {
    public static void copyProperties(Object source, Object target, String... ignoreProperties) {
        // 浅拷贝，只有变量类型和名字一样才拷贝
        BeanUtils.copyProperties(source, target, ignoreProperties);
    }

    // 对列表拷贝
    public static <T> List<T> copyList(List sources, Class<T> clazz) {
        List<T> targetList = new ArrayList<>();
        if (sources != null) {
            try {
                for (Object source : sources) {
                    T target = clazz.newInstance();
                    copyProperties(source, target);
                    targetList.add(target);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return targetList;
    }
}
